/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Objects.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author tinvukhac
 */
@Stateless(mappedName = "adminBean")
public class adminBean implements adminBeanRemote {

    String connectionString = "jdbc:sqlserver://TINVUKHACPC\\TINVUKHACSQL;"
            + "databaseName=imageSite";
    String serverUserName = "sa";
    String serverPasswd = "123456";

    @Override
    public List<User> getUserReportInfo() {
        List<User> userList = new ArrayList<User>();
        User tempUser;

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(connectionString, serverUserName, serverPasswd);

            PreparedStatement pstm = conn.prepareStatement("SELECT * FROM Users");
            ResultSet rs = pstm.executeQuery();

            while (rs.next()) {
                tempUser = new User();
                tempUser.setUserId(rs.getInt("userId"));
                tempUser.setUserName(rs.getString("userName"));
                tempUser.setWarnLevel(rs.getInt("warnLevel"));
                tempUser.setCurrentPoint(rs.getInt("currentPoint"));
                if (rs.getDate("expirationDate") != null) {
                    String expirationDateStr = rs.getDate("expirationDate").toString();
                    if(expirationDateStr.compareTo("2025-01-01") == 0)
                    {
                        tempUser.setExpirationDate("Block forever");
                    }
                    else
                    {
                        tempUser.setExpirationDate(expirationDateStr);
                    }            
                } else {
                    tempUser.setExpirationDate("Active user");
                }
                userList.add(tempUser);
            }
            pstm.close();
        } catch (Exception e) {
            //throw e;
        }
        return userList;
    }

    @Override
    public void blockUser(String userId, String type) {
        String expirationDateStr;
        Date expirationDate;
        DateFormat df;
        Calendar calendar = Calendar.getInstance();
        if (type.compareTo("1") == 0) {
            calendar.add(Calendar.DAY_OF_YEAR, 7);
            expirationDate = calendar.getTime();
            df = new SimpleDateFormat("yyyy-MM-dd");
            expirationDateStr = df.format(expirationDate);
        } else if (type.compareTo("2") == 0) {
            calendar.add(Calendar.DAY_OF_YEAR, 30);
            expirationDate = calendar.getTime();
            df = new SimpleDateFormat("yyyy-MM-dd");
            expirationDateStr = df.format(expirationDate);
        }
        else
        {
            expirationDateStr = "2025-01-01";
        }

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(connectionString, serverUserName, serverPasswd);

            PreparedStatement pstm = conn.prepareStatement("UPDATE Users SET currentPoint=0, expirationDate=?, warnLevel=warnLevel+1 WHERE userId=?");
            pstm.setString(1, expirationDateStr);
            pstm.setString(2, userId);
            pstm.executeUpdate();
            pstm.close();
        } catch (Exception e) {
            //throw e;
        }
    }
    
    @Override
    public void unblockUser(String userId)
    {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection conn = DriverManager.getConnection(connectionString, serverUserName, serverPasswd);

            PreparedStatement pstm = conn.prepareStatement("UPDATE Users SET expirationDate=NULL WHERE userId=?");
            pstm.setString(1, userId);
            pstm.executeUpdate();
            pstm.close();
        } catch (Exception e) {
            //throw e;
        }
    }
}
